/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2019
 */

package com.jzwl.site.manager.interaction.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jzwl.common.id.Sequence;
import com.jzwl.common.page.PageObject;
import com.jzwl.system.base.dao.BaseDAO;

@Repository("interactionDao")
public class BusinessCircleInteractionDao {

	@Autowired
	private BaseDAO baseDAO;//dao基类，操作数据库

	public boolean addBusinessCircleInteraction(Map<String, Object> map) {

		//自动注入时间戳为ID 酌情修改数据库类型为bigint  int会越界
		map.put("id", Sequence.nextId());
		map.put("createDate", new Date());
		map.put("isDelete", 0);
		String sql = "insert into business_circle_interaction "
				+ " (id,content,userName,phoneNumber,isDelete,createDate) " + " values "
				+ " (:id,:content,:userName,:isDelete,:createDate)";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public String getColumns() {
		return "" + " t.id as id," + " t.userId as userId," + " t.userName as userName,"
				+ " t.phoneNumber as phoneNumber," + " t.isDelete as isDelete," + " t.createDate as createDate";
	}

	public String getColumnsInfo() {
		return "" + " t.id as id," + "t.content as content," + " t.userId as userId," + " t.userName as userName,"
				+ " t.phoneNumber as phoneNumber," + " t.isDelete as isDelete," + " t.createDate as createDate";
	}

	public PageObject queryBusinessCircleInteractionList(Map<String, Object> map) {
		// [column]为字符串拼接, {column}为使用占位符. 如username='[username]',偷懒时可以使用字符串拼接 
		// [column] 为PageRequest的属性

		String sql = "select " + getColumns() + " from business_circle_interaction t where 1=1 ";

		if (null != map.get("content") && StringUtils.isNotEmpty(map.get("content").toString())) {
			sql = sql + " and t.content  = " + map.get("content") + "";
		}
		if (null != map.get("userId") && StringUtils.isNotEmpty(map.get("userId").toString())) {
			sql = sql + " and t.userId  = " + map.get("userId") + "";
		}
		if (null != map.get("userName") && StringUtils.isNotEmpty(map.get("userName").toString())) {
			sql = sql + " and t.userName  = " + map.get("userName") + "";
		}
		if (null != map.get("phoneNumber") && StringUtils.isNotEmpty(map.get("phoneNumber").toString())) {
			sql = sql + " and t.phoneNumber  = " + map.get("phoneNumber") + "";
		}
		if (null != map.get("isDelete") && StringUtils.isNotEmpty(map.get("isDelete").toString())) {
			sql = sql + " and t.isDelete  = " + map.get("isDelete") + "";
		}
		if (null != map.get("createDate") && StringUtils.isNotEmpty(map.get("createDate").toString())) {
			sql = sql + " and t.createDate >= " + map.get("createDateBegin") + "";
			sql = sql + " and t.createDate <= " + map.get("createDateEnd") + "";
		}

		sql = sql + " order by id ";

		PageObject po = baseDAO.queryForMPageList(sql, new Object[] {}, map);

		return po;
	}

	public boolean updateBusinessCircleInteraction(Map<String, Object> map) {

		String sql = "update v2_business_circle_interaction set "
				+ " id=:id,content=:content,userId=:userId,userName=:userName,phoneNumber=:phoneNumber,isDelete=:isDelete,createDate=:createDate "
				+ " where id=:id";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public boolean deleteBusinessCircleInteraction(Map<String, Object> map) {

		String sql = "delete from v2_business_circle_interaction where id in (" + map.get("id") + " ) ";

		return baseDAO.executeNamedCommand(sql, map);
	}

	@SuppressWarnings("unchecked")
	public Map<String, Object> getById(Map<String, Object> map) {

		Map<String, Object> resMap = new HashMap<String, Object>();

		String sql = "select " + getColumnsInfo() + " from business_circle_interaction t where t.id = " + map.get("id")
				+ "";

		resMap = baseDAO.queryForMap(sql);

		return resMap;

	}

}
